$(function() {
	
	$("#outHotelRegister").click(function(){
		if (confirm("您真的要退出注册吗?")) {
			window.location.href = "/onlinehotel/index.jsp";
		}
	});

	$("#hotelAccount").blur(function() {
		var hotelAccount = $("#hotelAccount").val();
		checkhotelAccount(hotelAccount)
	});
	
	$("#hotelPassword").blur(function() {
		var hotelPassword = $("#hotelPassword").val();
		checkhotelPwd(hotelPassword)
	});
	
	$("#hotelPassword2").blur(function() {
		var hotelPassword = $("#hotelPassword").val();
		var hotelPassword2 = $("#hotelPassword2").val();
		checkhotelPwd2(hotelPassword, hotelPassword2)
	});
	
	$("#hotelName").blur(function() {
		var hotelName = $("#hotelName").val();
		checkhotelName(hotelName)
	});
	
	$("#startTime").blur(function() {
		var startTime = $("#startTime").val();
		var EndTime = $("#EndTime").val();
		checkTime(startTime,EndTime)
	});
	
	$("#EndTime").blur(function() {
		var startTime = $("#startTime").val();
		var EndTime = $("#EndTime").val();
		checkTime(startTime,EndTime)
	});
	
	$("#hotelBoss").blur(function() {
		var hotelBoss = $("#hotelBoss").val();
		checkhotelBosse(hotelBoss)
	});
	
	$("#phone").blur(function() {
		var phone = $("#phone").val();
		checkphone(phone)
	});
		
		
	
	$("#hotelAccount").focus(function() {
		hotelAccountSpan.innerHTML = "<font size=2 color=red>·为了方便记忆,您可用手机号注册!</font>";
	});

	
	$("#btnHotel").click(function(){
		var flag = true;
		checkhotelAccount($("#hotelAccount").val());
		if (!checkhotelPwd($("#hotelPassword").val() )) {
			flag = false;
		}
		if (!checkhotelPwd2($("#hotelPassword").val(),$("#hotelPassword2").val() )) {
			flag = false;
		}
		if (!checkhotelName($("#hotelName").val())) {
			flag = false;
		}
		if (!checkTime($("#startTime").val(),$("#EndTime").val())) {
			flag = false;
		}
		if (!checkhotelBoss($("#hotelBoss").val())) {
			flag = false;
		}
		if (!checkphone($("#phone").val())) {
			flag = false;
		} 
		
		if(flag){ 
			var senderAccount = $("#hotelAccount").val();
			var zz = /^\w{4,18}$/
			if (zz.test(hotelAccount)){
				$.ajax({
				type : "post",
				url : "hotel.do",
				data : "method=isExist&hotelAccount="+hotelAccount,
				cache : false,
				success:function(msg){			
					if("true"==msg){
						hotelAccountSpan.innerHTML = "<font size=2 color=red>账号已使用</font>";
					}else{
						hotelAccountSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";				
						$("#hotel").submit();
					}
				}
				});		
			} else {
				hotelAccountSpan.innerHTML = "<font size=2 color=red>·请输入4-18个字符,包括字母、数字、下划线!</font>";
			}
		}
	});
});

function checkhotelAccount(hotelAccount){
	var zz = /^\w{4,18}$/
	if (zz.test(hotelAccount)){
		alert(hotelAccount);
		$.ajax({
		type : "post",
		url : "sender.do",
		data : "method=isExist&hotelAccount="+hotelAccount,
		cache : false,
		success:function(msg){
			
			if("true"==msg){
				hotelAccountSpan.innerHTML = "<font size=2 color=red>账号已使用</font>";
				return false;
			}else{
				hotelAccountSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
				var z = /^1[3|4|5|8][0-9]\d{8}$/
				if(z.test(hotelAccount)){
					var phone = document.getElementById("phone");
					phone.value = hotelAccount;
					phoneSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
				}
				return true;
			}
		}
		});		
	} else {
		hotelAccountSpan.innerHTML = "<font size=2 color=red>·请输入4-18个字符,包括字母、数字、下划线!</font>";
		return false;
	}
}

function checkhotelPwd(hotelPassword) {
	var zz = /^\w{4,16}$/
	if (zz.test(hotelPassword)) {
		hotelPasswordSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
		return true;
	} else {
		hotelPasswordSpan.innerHTML = "<font size=2 color=red>·请输入4-16个字符,包括字母、数字、下划线!</font>";
		return false;
	}
}

function checkhotelPwd2(hotelPassword,hotelPassword2){
	if (hotelPassword != hotelPassword2) {
		hotelPassword2Span.innerHTML = "<font size=2 color=red>·对不起,两次密码不一致!</font>";
		return false;
	} else {
		hotelPassword2Span.innerHTML = "<font size=2 color=blue>·OK!</font>";
		return true;
	}
}

function checkhotelName(hotelName){
	
	var zz = /[\u4e00-\u9fa5]/
	if (zz.test(hotelName)) {
		hotelNameSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
		return true;
	} else {
		hotelNameSpan.innerHTML = "<font size=2 color=red>·对不起,请输入正确的饭店名!</font>";
		return false;
	}
	
}

function checkTime(startTime,EndTime){
	
	
	if(startTime == null || startTime == ""){
		TimeSpan.innerHTML = "<font size=2 color=red>·请选择贵饭店的营业时间!</font>";
		return false;
	}else if(EndTime == null || EndTime == ""){
		TimeSpan.innerHTML = "<font size=2 color=red>·请选择贵饭店的营业时间!</font>";
		return false;
	}else{
		TimeSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
		return true;
	}
	
}

function checkhotelBoss(hotelBoss){
	
	var zz = /[\u4e00-\u9fa5]/
	if (zz.test(hotelBoss)) {
		hotelBossSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
		return true;
	} else {
		hotelBossSpan.innerHTML = "<font size=2 color=red>·对不起,请正确填写负责人姓名!</font>";
		return false;
	}
	
}

function checkphone(phone){
	
	var zz = /\d{3}-\d{8}|\d{4}-\d{7}|^1[3|4|5|8][0-9]\d{8}$/
	if (zz.test(phone)) {
		phoneSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
		return true;
	} else {
		phoneSpan.innerHTML = "<font size=2 color=red>·请填写正确的固定电话或者手机号码!</font>";
		return false;
	}
	
}